package com.allwees.bs.module.fms.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.BranchUser;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.exception.AuthException;
import com.allwees.bs.core.modelbase.exception.AuthExceptionEnum;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.fms.entity.MerchantWithdrawAccount;
import com.allwees.bs.module.fms.req.MerchantWithdrawAccountInfoReq;
import com.allwees.bs.module.fms.req.SetDefaultWithdrawReq;
import com.allwees.bs.module.fms.req.query.MerchantWithdrawAccountQueryReq;
import com.allwees.bs.module.fms.service.MerchantWithdrawAccountService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商户提现账户操作
 *
 * @author yangylsky
 * @since 2019-12-30
 *
 */
@RestController
@RequestMapping("/backstage/api/fms/merchant_withdraw_account")
public class MerchantWithdrawAccountApi extends BoCrudApi<MerchantWithdrawAccount, MerchantWithdrawAccountService> {
    /**
     * 创建个人提现账户
     *
     * @param req
     * @return
     */
	@PostMapping("/create_individual_account")
	@DynamicInject(type = DynamicInject.Type.Retailer, key = "branchUuid")
	public R createIndividualAcount(@RequestBody MerchantWithdrawAccountInfoReq req) {
		MerchantWithdrawAccount account = boService.createIndividualAccount(req);
        return R.ok("创建个人提现账户成功").put("uuid", account.getUuid());
	}

    /**
     * 创建公司提现账户
     *
     * @param req
     * @return
     */
	@PostMapping("/create_company_account")
	public R createCompanyAcount(@RequestBody MerchantWithdrawAccountInfoReq req) {
		MerchantWithdrawAccount account = boService.createCompanyAccount(req);
        return R.ok("创建公司提现账户成功").put("uuid", account.getUuid());
	}

	@Override
	protected Object _doRetrieve(String uuid) {
		if(EBranchType.PLATFORM != getCurrentUser().getBranchType()) {
			throw new AuthException(AuthExceptionEnum.NO_AUTHORIZE);
		}
		return super._doRetrieve(uuid);
	}

	/**
	 * 查询默认提现账户
	 *
	 * @return
	 */
	@GetMapping("/default")
	public R findDefault() {
		BranchUser user = getCurrentUser();
		MerchantWithdrawAccount account = boService.findDefaultWithdrawAccount(user.getBranchType(), user.getBranchUuid());
        return R.ok(account);
	}

	/**
	 * 查询默认提现账户
	 *
	 * @return
	 */
	@PutMapping("/set_default")
	public R setDefault(@RequestBody SetDefaultWithdrawReq req) {
		boService.setDefaultWithdrawAccount(req);
        return R.ok("设置成功");
	}

	/**
	 * 查询所有提现账户
	 *
	 * @return
	 */
	@GetMapping("/list")
	public R findAllWithdrawAccount() {
		BranchUser user = getCurrentUser();
		List<MerchantWithdrawAccount> accounts = boService.findAllWithdrawAccount(user.getBranchType(), user.getBranchUuid());
        return R.ok(accounts);
	}

    /**
     * 查询提现账户分页列表
     *
     * @param page
     * @param req
     * @return
     */
    @GetMapping("/page")
    public R page(Page<MerchantWithdrawAccount> page, MerchantWithdrawAccountQueryReq req) {
        IPage<MerchantWithdrawAccount> retPage = boService.findPage(page, req);
        return R.ok(retPage);
    }
}
